﻿using System;
using UIFramwork.Factory;
using UIFramwork.Factory.Component;
using UnityEngine;


namespace UIFramwork
{
    /// <summary>
    /// 基类|UI
    /// </summary>
    public abstract class UIBase : MonoBehaviour, IUILife
    {
        #region UI 生命周期
        public virtual void CreateUI()
        {
            this.SetMask();
            this.SetAreaQuit();

            this.OnUICreated?.Invoke();
        }
        public virtual void ActiveUI()
        {
            this.OnUIActived?.Invoke();
        }
        public virtual void StartUI()
        { 
        
        }
        public virtual void DeActiveUI()
        {
            this.OnUIDeactived?.Invoke();
        }
        public virtual void DisposeUI()
        {
            this.OnUIDisposed?.Invoke();
        }
        #endregion

        #region UI 特性
        [HideInInspector, Tooltip("模块|区域阻挡")] public bool UseAreaQuit = false;
        [HideInInspector, Tooltip("模块|使用遮罩")] public bool UseMask = false;
        [HideInInspector, Tooltip("模块|作为单例")] public bool UseAsSingle = false;
        [HideInInspector, Tooltip("模块|关旧通知")] public bool UseCloseOld = false;
        [HideInInspector, Tooltip("布局|使用四周")] public bool UseQuardRato = false;

        [HideInInspector, Tooltip("模块|关旧允许")] public bool CanBeClosed = false;
        [HideInInspector, Tooltip("模块|历史队列")] public bool CanBeRecord = false;
        #endregion

        #region UI 事件
        [Tooltip("事件|页面创建时")] public event Action OnUICreated;
        [Tooltip("事件|页面激活时")] public event Action OnUIActived;
        [Tooltip("事件|页面禁用时")] public event Action OnUIDeactived;
        [Tooltip("事件|页面释放时")] public event Action OnUIDisposed;
        #endregion

        #region UI 属性
        /// <summary>
        /// 设置|使用遮罩
        /// </summary
        public void SetMask()
        {
            if (!UseMask) return;

            GameObject go = UIComponentFactory.GetMask();
            UIMask uiMask = go.GetComponent<UIMask>();

            uiMask.Init();
        }

        /// <summary>
        /// 设置|使用阻挡层
        /// </summary>
        public void SetAreaQuit()
        {
            if (!UseAreaQuit) return;

            GameObject go = Resources.Load<GameObject>("UI Prefab/UseAreaQuit");
            RectTransform rect = go.GetComponent<RectTransform>();
            {
                rect.offsetMax = Vector2.zero;
                rect.offsetMin = Vector2.zero;
                rect.SetAsFirstSibling();
            }
        }
        #endregion


        public virtual void InitComponent() { }
    }
}